#include "nstar_ts_pack.h"
#include <string.h>

#define NEVIF_CMD_PID  6692 /*指令PID */
const unsigned char tmp_id[6]={0x95,0x11,};
#define NEVIF_CMD_CTLID  tmp_id
#define NEVIF_CMD_PGNUM  7199   /*节目号 syscfg_get_serid*/



/*------------------*/

#define ENABLE_TS_PACK_DEBUG
#ifdef ENABLE_TS_PACK_DEBUG
#define TS_PACK_DEBUG 	nstar_adt_printk
#else
#define TS_PACK_DEBUG(...) do{}while(0)
#endif


enum {
	LOAD_FLG_INVALID = 0,
	LOAD_FLG_PG = 1,
	LOAD_FLG_PLAY =0X2,
	LOAD_FLG_PLAY_STOP = 0X4,
};
#define NSTAR_SWITCH_STATUS_HOLD				0//淇濇寔
#define NSTAR_SWITCH_STATUS_OPEN				1//鎵撳紑
#define NSTAR_SWITCH_STATUS_STOP				2//鍏抽棴
#define NSTAR_SWITCH_STATUS_DRILL				3//婕旂粌

static unsigned char g_playDta[188];

static volatile unsigned char g_loadFlg = LOAD_FLG_INVALID;
static volatile unsigned char g_playStopCnt = 0;
static unsigned short g_playSerid;
static unsigned char g_playEmFlg = 0;

/*
47 
5A 23 pid |=0x40
10 00 93 //固定
00 07 
F9 //固定
01  //PLAY
06
13 02 23 01 FF FF//LOGIC
50 00 67//OK
*/

static unsigned char _load_head(unsigned char*p_ts)
{
	unsigned short pid;
	pid = NEVIF_CMD_PID; /*指令pid*/
    p_ts[0] = 0x47;
	p_ts[1] = (pid>>8)|0x40;
	p_ts[2] = pid&0xff;
	p_ts[3] = 0x10;
	p_ts[4] = 0;
	p_ts[5] = 0x93;
	p_ts[8] = 0xf9;
	return 9;
}
static void _ts_set_pack_len_crc(unsigned char*p_ts,unsigned char len)
{
	unsigned int crc32;
	p_ts[6] = 0;
	p_ts[7] = len-4;
	len -= 5; 
	p_ts+=5;
	crc32 = nstar_crc32_ts(p_ts,len);
}

static void _set_empty_pack(unsigned char*p_dta)
{
	unsigned char len;
	p_dta[0] = 0x47;
	len = _load_head(p_dta);
	p_dta[len++] = 0;
	p_dta[len++] = 0;
	_ts_set_pack_len_crc(p_dta,len);
	//nstar_adt_memset(p_dta+4,0xff,184);
}
void nstar_ts_pack_init(void)
{
	_set_empty_pack(g_playDta);
}



static unsigned char _ts_pack_play(unsigned short serid)
{
	unsigned char ret = 0;
	unsigned char*p_pack = g_playDta;
	unsigned char len;
	len = _load_head(p_pack);
	p_pack[len++] = 1;
	p_pack[len++] = 6;	
	
	//sys_status_logic_addr_get(p_pack+len);
	memcpy(p_pack+len, NEVIF_CMD_CTLID, 6);
    
	len += 6;
	p_pack[len++] = (NSTAR_SWITCH_STATUS_OPEN << 6) | (g_playEmFlg<<5) | (4<<2);
	p_pack[len++] = g_playSerid>>8;
	p_pack[len++] = g_playSerid&0xff;
	_ts_set_pack_len_crc(p_pack,len);
	ret = 1;	
	return ret;
}

static unsigned char _ts_pack_play_stop(void)
{
	unsigned char*p_pack = g_playDta;
	p_pack[17] = (p_pack[17] &0x3f) | (NSTAR_SWITCH_STATUS_STOP<<6);
	_ts_set_pack_len_crc(p_pack,20);
	return 1;
}

static unsigned char _try_load_dta(void)
{
	unsigned char ret = 0;
	unsigned char loadFlg = g_loadFlg;
	if(loadFlg > LOAD_FLG_INVALID ){
		g_loadFlg = LOAD_FLG_INVALID;
		if(loadFlg& LOAD_FLG_PLAY){
			g_playStopCnt = 0;
            //printf("ts set pack play\n");
			_ts_pack_play(g_playSerid);
		}
		if(loadFlg& LOAD_FLG_PLAY_STOP){
            //printf("ts set pack stop\n");
			_ts_pack_play_stop();
			g_playStopCnt = 3;
		}
		ret = 1;
	}
	return ret;
}

unsigned char* nstar_ts_pack_get_snd_dta(unsigned int *p_outSndLen)
{
	*p_outSndLen = 188;
	if(0 == _try_load_dta()){
		if(g_playStopCnt){
			g_playStopCnt--;
			if(g_playStopCnt == 0){
                //printf("ts set pack empty\n");
				_set_empty_pack(g_playDta);
			}
		}
	}
	return g_playDta;
}


unsigned char nstar_pack_tcp_passwd(unsigned char *buf, unsigned char buflen, unsigned char type)
{
#define NET_PG_PASSWD 		"new star"
#define NET_PG_PASSWD_LEN 	8
	unsigned char len;
	if(buflen < 20)
        return 0;
	buf[0]= NET_PG_PASSWD_LEN;
	memcpy(buf+1, NET_PG_PASSWD, NET_PG_PASSWD_LEN);
	len= NET_PG_PASSWD_LEN+1;
	if(type > 0 ){
		buf[len] = type;
		len ++;
	}
	if(type == PROID_MANUAL){
		unsigned short sid= NEVIF_CMD_PGNUM;
		buf[len]= sid >> 8;
		len ++;
		buf[len]= sid &0xff;
		len ++;
	}
    return len;
}


void nstar_ts_pack_play(unsigned char emflg)
{
	g_playSerid = NEVIF_CMD_PGNUM;
	g_playEmFlg = emflg&1;
	if( g_loadFlg & LOAD_FLG_PLAY_STOP){
		g_loadFlg &= ~LOAD_FLG_PLAY_STOP;
	}
	g_loadFlg |= LOAD_FLG_PLAY;
}

void nstar_ts_pack_play_stop(void)
{
	g_loadFlg |= LOAD_FLG_PLAY_STOP;
}


